package com.nand.module.user.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.test.internal.util.LogUtil;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.lzy.okgo.model.HttpParams;
import com.nand.library.base.base.BaseActivity;
import com.nand.library.base.base.BaseApp;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.Callback;
import com.nand.library.base.http.MyCallback;
import com.nand.library.base.http.OkGoBuilder;
import com.nand.library.base.http.OkGoUtils;
import com.nand.library.base.utils.SharedPreferencesUtils;
import com.nand.library.base.utils.SpUtils;
import com.nand.module.user.R;
import com.nand.module.user.bean.UserInfoBean;
import com.nand.module.user.constant.URL;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/user/login")
public class LoginActivity extends BaseActivity {

    private EditText etName;
    private EditText etPassword;
    private String mPassword;
    private String mName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }


    @Override
    protected int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        setStatusBar(R.color.transparent);

        String name = SpUtils.readString(this, "account", "");
        etName = findViewById(R.id.ev_name);
        etPassword = findViewById(R.id.ev_pwd);
        etName.setText(name);
        findViewById(R.id.tv_).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showToast("暂未开通此功能！");
            }
        });
        findViewById(R.id.tv_z).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showToast("暂未开通此功能！");
            }
        });

        findViewById(R.id.bt_login).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                mName = etName.getText().toString().trim();
                mPassword = etPassword.getText().toString().trim();

               /* if ("".equals(mName) || "".equals(mPassword)) {
                    showToast("账号或密码不能为空！");
                } else {*/
               Log.e("aaaa","aaaaaa");
                    onLogin();
             //   }
            }
        });
    }

    @Override
    protected void initData() {

    }

    ArrayList<String> mAuth = new ArrayList<>();

    private void onLogin() {
        HttpParams paramsPost = new HttpParams();
        paramsPost.put("account", mName);
        paramsPost.put("password", mPassword);


        OkGoUtils.getInstance().post(this, URL.LOGIN, paramsPost, 1, true, new MyCallback() {
            @Override
            public void onSuccess(ResponseBean response, int id) {
                if (response.getCode() == 200) {
                    Log.e("12TAG:",response.toString());
                    Log.e("13TAG:",response.getDataInfo().toString());
                    String data = response.getDataInfo().toString();
                    UserInfoBean userInfoBean = JSON.parseObject(data, UserInfoBean.class);
                    SharedPreferencesUtils.setParam(LoginActivity.this, "userInfo", response.getDataInfo().toString());
                    SharedPreferencesUtils.setParam(LoginActivity.this, "userId", userInfoBean.getUserId());
                    SpUtils.writeString(LoginActivity.this, "account", mName);
                    SpUtils.writeInt(LoginActivity.this, "userId", userInfoBean.getUserId());
                    SpUtils.writeString(LoginActivity.this, "userName", userInfoBean.getUserName());
                    SpUtils.writeString(LoginActivity.this, "departMentName", userInfoBean.getDepartMentName());
                    String[] setAuth = userInfoBean.getSetAuth();
                    for (int i = 0; i < setAuth.length; i++) {
                        mAuth.add(i,setAuth[i]);
                    }
                    SpUtils.putListData( "setAuth", mAuth);

                    ARouter.getInstance().build("/app/main").navigation();
                    finish();
                } else {
                    showToast(response.getMsg());
                }
            }

            @Override
            public void onError(Throwable e, int id) {
                Log.e("aaaa","bbb");
                showToast("登陆失败");
            }
        });

        Log.e("aaaa","ccc");
    }



    /**
     * 该方法用来捕捉手机键盘被按下的事件。
     *
     * @param keyCode  该参数指的是被按下的键的键盘码，手机键盘中每个按钮都会有其对应的键盘码，
     * 在应用程序都是通过键盘码才知道用户按下的是哪个键。
     * @param event    当用户按下按键时，系统会自动将事件封装成KeyEvent对象供应用程序使用。
     * 按键事件KeyEvent 按键事件有多种常量类型，比如 KEYCODE_BACK
     * @return
     */
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //判断用户两次按键的时间差是否在一个预期值之内，是的话直接直接退出，不是的话提示用户再按一次后退键退出。
            if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
                //当返回true时，表示已经完整地处理了这个事件，并不希望其他的回调方法再次进行处理，而当返回false时，
                // 表示并没有完全处理完该事件，更希望其他回调方法继续对其进行处理，
                return true;
            } else {
                 //结束当前activity
                BaseApp.getApplication().getActivityManage().finishAll();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}
